package com.diandireader.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.diandireader.activity.R;
import com.diandireader.domain.DiandiReaderVO;
import com.diandireader.util.FileHelper;
import com.diandireader.util.StringHelper;
/**
 * Filebrows
 * @author mayh
 *
 */
public class FileBrowsActivity extends Activity {

	private ListView listView;
	private List<File> fileList;
	private File[] fileArray;
	private FileAdapter adapter;
	private File currentFile;
	private TextView currentPathView;
	private TextView goBackView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//      去掉标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
//      去掉状态栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.reader_databrows);
		fileList = new ArrayList<File>();
		setViews();
		setListeners();
		adapter = new FileAdapter();
		listView.setAdapter(adapter);
		File file = new File("/");
		refreshFile(file);
	}
	
	public void setViews(){
		listView = (ListView)findViewById(R.id.filelist);
		currentPathView = (TextView)findViewById(R.id.filebrows_path);
		goBackView = (TextView)findViewById(R.id.goback);
	}
	public void setListeners(){
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				currentFile = fileList.get(position);
				if(currentFile.isDirectory()){
					refreshFile(currentFile);
				}else {
					//判断文件格式支不支持
					String fileType = FileHelper.getFileSuffix(currentFile);
					if(!FileHelper.allowTypeList.contains(fileType)){
						Toast.makeText(FileBrowsActivity.this, "暂不支持该格式", Toast.LENGTH_SHORT).show();
						return;
					}
					//save the book,not distinguish the two file with the same name
					SharedPreferences preference = getSharedPreferences(DiandiReaderVO.BOOKPOSITION, Context.MODE_PRIVATE);
					Editor edit = preference.edit();
					edit.putString(currentFile.getName(), currentFile.getPath());
					edit.commit();
					//open the book
					Intent intent = new Intent();
					intent.putExtra("filePath", currentFile.getPath());
					intent.setClass(FileBrowsActivity.this, SimpleTextReaderActivity.class);
					startActivity(intent);
					finish();
				}
			}
		});
		goBackView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(currentFile!=null&&currentFile.getParent()!=null)
				refreshFile(new File(currentFile.getParent()));
			}
		});
	}
	
	public void refreshFile(File file){
		if(file!=null){
			currentFile = file;
			fileArray = file.listFiles();
			if(fileArray!=null){
				currentPathView.setText(file.getPath());
				fileList = Arrays.asList(fileArray);
				adapter.notifyDataSetChanged();
			}else {
				Toast.makeText(FileBrowsActivity.this, "无权限", Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	//fileListAdapter
	class FileAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return fileList.size();
		}

		@Override
		public Object getItem(int position) {
			return fileList.get(position);
		}

		@Override
		public long getItemId(int id) {
			return id;
		}

		@Override
		public View getView(int position, View view, ViewGroup arg2) {
			File file = fileList.get(position);
			view = View.inflate(FileBrowsActivity.this, R.layout.filelistitem, null);
			FileInfo.name = (TextView)view.findViewById(R.id.filename);
			FileInfo.time = (TextView)view.findViewById(R.id.createtime);
			FileInfo.icon = (ImageView)view.findViewById(R.id.fileicon);
			if(!file.isDirectory()){
				FileInfo.icon.setImageResource(R.drawable.file);
			}
			FileInfo.name.setText(file.getName());
			FileInfo.time.setText(StringHelper.dateToString(new Date(file.lastModified()), "yyyy-MM-dd HH:ss"));
			return view;
		}
		
	}
	static class FileInfo {
		static TextView name;
		static TextView time;
		static ImageView icon;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			if(currentFile!=null&&currentFile.getParent()!=null){
				refreshFile(new File(currentFile.getParent()));
				return true;
			}
			else 
				return super.onKeyDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	}
}
